function sortObj(obj) {
    var arr = [];
    for (let i = obj.length; i > 0; i--) {
        arr.push(obj[i - 1]);
    }

    return arr;
}

function getHeaderObj(header_data) {
    let header_data_array = header_data.split('\n');
    let header_obj = {};
    let is_multi_value = false;
    let multi_value_key = '';
    for (let i = 0; i < header_data_array.length; i++) {
        let item = header_data_array[i];
        if (header_data_array[i].length > 0) {
            let key = item.substr(0, item.indexOf(':'));
            let value = item.substr(item.indexOf(':') + 1);
            if (key == 'categories' || key == 'tags' || key == 'sync_links' || (is_multi_value && !key)) {
                if (!is_multi_value || key == 'categories' || key == 'tags' || key == 'sync_links') {
                    multi_value_key = key;
                    header_obj[multi_value_key] = new Array();
                } else {
                    let sub_value = item.split(' ')[1];
                    header_obj[multi_value_key].push(sub_value);
                }
                is_multi_value = true;

            } else {
                is_multi_value = false;
                header_obj[key] = value;
            }
        }
    }

    return header_obj;
}



function getMdList(md_data) {
    var md_content_list = md_data.split('---');
    return md_content_list; // 1: header; 2:content;
}

module.exports = {
    getHeaderObj: getHeaderObj,
    getMdList: getMdList,
    sortObj: sortObj
}